{%- comment -%}

The following machine properties are supported in the 'machines' Front Matter of PCjs Markdown pages:

    'id' (eg: ibm5150)
    'name' (eg: "IBM PC (Model 5150) with Monochrome Display")
    'type' (eg: c1p, pcx86, pcx80, pdp10, pdp11, leds, ti42, ti55, ti57) ["-dbg" suffixes are DEPRECATED]
    'debugger' (default: true) [DEPRECATED along with the "-dbg" suffixes on 'type'; all debuggers are built-in now]
    'class' (eg: "machine-left", "machine-right", etc; default: none)
    'config' (default: machine.xml)
    'template' (default: machine.xsl)
    'debug' (default: false; true enables asserts and is much slower)
    'unbundled' (default: false; true to include unbundled source files)
    'uncompiled' (default: false; true to include the bundled but uncompiled sources)
    'parms' (stringified object that collects additional machine properties listed below)

Note that if you want BACKTRACK support enabled in a machine, 'uncompiled' must be true, because that isn't
currently compiled into any machines.  Also note that while multiple machines ARE supported on a single page, there
are some limitations; for example, loading two or more machines of the same 'type' but with different 'debug',
'debugger' and/or 'uncompiled' settings may not work properly.

The following optional machine properties will be added to the 'parms' property:

    'autoMount' (eg: {"A":{"name":"OS/2 FOOTBALL (v7.68.17)","path":"/disks/diskettes/pcx86/sys/os2/misc/87058/FOOTBALL-76817.json"}})
    'autoScript' (eg, type Keyboard "$date\r$time\r")
    'autoStart' (eg: true)
    'autoType' (eg: $date\r$time\r)
    'drives' (eg: '[{name:"68Mb Hard Disk",type:4,path:"/harddisks/pcx86/drives/68mb/WIN95.json"}]')
    'cdromDrives' (eg: '[{name:"CD",type:0,path:"/disks/cdroms/cds005/microsoft/reference/Bookshelf-Windows-1991/cdrom"}]')
    'floppyDrives' (eg: '[{boot:false}]')
    'resume' (eg: 1)
    'sound' (eg: true)
    'state' (eg: state.json)
    'flicker' (eg: 0 to disable CGA flicker)
    'sizeRAM' (eg, 640 for 640K of base memory)
    'testRAM' (eg, false)
    'commands' (debugger commands, if any)
    'messages' (eg: fault)
    'connection' (eg: serialPort->vt100a.serialPort)

Note that the 'resume' and 'state' properties were originally (and still are) Computer component properties, but the machine.html
template doesn't add them to the Computer component anymore, since we started supporting them here as machine "override" properties.

{%- endcomment -%}

{%- for candidate in page.machines -%}
  {%- assign machine_id = candidate.id -%}
  {%- capture machine_type -%}{{ candidate.type | remove: "-dbg" }}{%- endcapture -%}
  {%- if candidate.components or candidate.config -%}
    {%- assign machine = candidate -%}
  {%- else -%}
    {%- assign machine_group = site.data.machines[machine_type].folder | split: '/' -%}
    {%- unless machine_group[1] %}
      {%- assign machine = site.data[machine_type][candidate.id] -%}
    {%- else %}
      {%- assign machine_group = machine_group[0] -%}
      {%- assign machine = site.data[machine_group][machine_type][candidate.id] -%}
    {%- endunless %}
  {%- endif -%}
  {%- unless machine -%}
    {%- continue -%}
  {%- endunless -%}
  {%- comment -%}

    On the PCjs web server, the site.data folder contains a machines.json, which includes machine data for all available machine types.

    For other servers, we support an alternative: the same data may be stored inside a 'machine' property of the machine's configuration file.

    The only machine data properties we really need are: 'version', 'factory', and 'script'.  Other properties such as 'folder', 'copy', and
    'scripts' (which allows multiple unbundled scripts per machine) are only supported on the PCjs web server.

  {%- endcomment -%}
  {%- assign machine_data = site.data.machines[machine_type] -%}
  {%- unless machine_data -%}
    {%- assign machine_data = site.data -%}
    {%- assign machine_paths = machine.config | replace: "/machines/","" | replace: ".json","" | split: "/" -%}
    {%- for machine_path in machine_paths -%}{%- assign machine_data = machine_data[machine_path] -%}{%- endfor -%}
    {%- assign machine_data = machine_data.machine -%}
  {%- endunless -%}

  {%- assign machine_folder = machine_data.folder -%}
  {%- if machine_data.copy != nil -%}
    {%- assign machine_data = site.data.machines[machine_data.copy] -%}
    {%- unless machine_folder -%}
      {%- assign machine_folder = machine_data.folder -%}
    {%- endunless -%}
  {%- endif -%}
  {%- unless machine_folder -%}
    {%- assign machine_folder = machine_type -%}
  {%- endunless -%}
  {%- if machine_data.version != nil -%}
    {%- capture machine_version -%}{{ machine_data.version }}{%- endcapture -%}
  {%- else -%}
    {%- capture machine_version -%}{{ site.data.machines.shared.version }}{%- endcapture -%}
  {%- endif -%}
  {%- capture machine_factory -%}{{ machine_data.factory }}{%- endcapture -%}
  {%- assign machine_config = "" -%}
  {%- assign machine_template = "" -%}
  {%- unless machine.components or machine.config contains ".json" and machine_factory contains "embed" -%}
    {%- unless machine.config -%}
      {%- assign machine_config = "machine.xml" -%}
    {%- else -%}
      {%- assign machine_config = machine.config -%}
    {%- endunless -%}
  {%- endunless -%}
  {%- if machine.autoMount == nil -%}
    {%- assign machine_autoMount = "" -%}
  {%- else -%}
    {%- capture machine_autoMount -%},autoMount:{{ machine.autoMount | jsonify | replace: "'", "\\'" }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.autoScript == nil -%}
    {%- assign machine_autoScript = "" -%}
  {%- else -%}
    {%- capture machine_autoScript -%},autoScript:"{{ page.machineScripts[machine.autoScript] | strip_newlines | replace:'"':"\\\\&quot;" | replace:"\r":"\\\\r" | replace:"\n":"\\\\n" | replace:"\t":"\\\\t" | replace:"\x":"\\\\x" }}"{%- endcapture -%}
  {%- endif -%}
  {%- if machine.autoStart == nil -%}
    {%- assign machine_autoStart = "" -%}
  {%- else -%}
    {%- capture machine_autoStart -%},autoStart:{{ machine.autoStart }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.autoType == nil -%}
    {%- assign machine_autoType = "" -%}
  {%- else -%}
    {%- capture machine_autoType -%},autoType:"{{ machine.autoType | replace:"\\":"\\\\\\\\" | replace:"\r":"\\\\r" | newline_to_br | replace:"<br />":"\\\\r" | strip_newlines | replace:"\n":"\\\\n" | replace:"\t":"\\\\t" | replace:"\x":"\\\\x" | replace:"&":"&amp;" | replace:"<":"&lt;" | replace:">":"&gt;" }}"{%- endcapture -%}
  {%- endif -%}
  {%- if machine.class == nil -%}
    {%- assign machine_class = "" -%}
  {%- else -%}
    {%- capture machine_class -%},'{{ machine.class }}'{%- endcapture -%}
  {%- endif -%}
  {%- if machine.resume == nil -%}
    {%- assign machine_resume = "" -%}
  {%- else -%}
    {%- capture machine_resume -%},resume:"{{ machine.resume }}"{%- endcapture -%}
  {%- endif -%}
  {%- if machine.sound == nil -%}
    {%- assign machine_sound = "" -%}
  {%- else -%}
    {%- capture machine_sound -%},sound:{{ machine.sound }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.state == nil -%}
    {%- assign machine_state = "" -%}
  {%- else -%}
    {%- capture machine_state -%},state:"{{ machine.state }}"{%- endcapture -%}
  {%- endif -%}
  {%- if machine.flicker == nil -%}
    {%- assign machine_flicker = "" -%}
  {%- else -%}
    {%- capture machine_flicker -%},flicker:{{ machine.flicker }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.sizeRAM == nil -%}
    {%- assign machine_sizeRAM = "" -%}
  {%- else -%}
    {%- capture machine_sizeRAM -%},sizeRAM:{{ machine.sizeRAM }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.testRAM == nil -%}
    {%- assign machine_testRAM = "" -%}
  {%- else -%}
    {%- capture machine_testRAM -%},testRAM:{{ machine.testRAM }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine.commands == nil -%}
    {%- assign machine_commands = "" -%}
  {%- else -%}
    {%- capture machine_commands -%},commands:"{{ machine.commands | replace:"&":"&amp;" | replace:"#":"&#35;" | replace:'"':"\\\\&quot;" }}"{%- endcapture -%}
  {%- endif -%}
  {%- if machine.messages == nil -%}
    {%- assign machine_messages = "" -%}
  {%- else -%}
    {%- capture machine_messages -%},messages:"{{ machine.messages }}"{%- endcapture -%}
  {%- endif -%}
  {%- assign machine_hdrives = "" -%}
  {%- if machine.drives != nil -%}
    {%- if machine.drives == "" -%}
      {%- assign machine_hdrives = ",drives:[]" -%}
    {%- else -%}
      {%- capture machine_hdrives -%},drives:{{ machine.drives }}{%- endcapture -%}
    {%- endif -%}
  {%- endif -%}
  {%- if machine.cdromDrives != nil -%}
    {%- if machine.cdromDrives == "" -%}
      {%- assign machine_cdroms = ",cdromDrives:[]" -%}
    {%- else -%}
      {%- capture machine_cdroms -%},cdromDrives:{{ machine.cdromDrives }}{%- endcapture -%}
    {%- endif -%}
  {%- else -%}
    {%- assign machine_cdroms = "" -%}
  {%- endif -%}
  {%- assign machine_diskettes = "" -%}
  {%- if machine.diskettes != nil -%}
    {%- capture machine_diskettes -%},diskettes:"{{ machine.diskettes }}"{%- endcapture -%}
  {%- endif -%}
  {%- assign machine_fdrives = "" -%}
  {%- if machine.floppyDrives != nil -%}
    {%- if machine.floppyDrives == "" -%}
      {%- assign machine_fdrives = ",floppyDrives:[]" -%}
    {%- else -%}
      {%- capture machine_fdrives -%},floppyDrives:{{ machine.floppyDrives }}{%- endcapture -%}
    {%- endif -%}
  {%- endif -%}
  {%- if machine.connection == nil -%}
    {%- assign machine_connection = "" -%}
  {%- else -%}
    {%- capture machine_connection -%},connection:"{{ machine.connection }}"{%- endcapture -%}
  {%- endif -%}
  {%- capture machine_parms -%}{{ machine_autoMount }}{{ machine_autoScript }}{{ machine_autoStart }}{{ machine_autoType }}{{ machine_cdroms }}{{ machine_diskettes }}{{ machine_fdrives }}{{ machine_hdrives }}{{ machine_resume }}{{ machine_sound }}{{ machine_state }}{{ machine_flicker }}{{ machine_sizeRAM }}{{ machine_testRAM }}{{ machine_commands }}{{ machine_messages }}{{ machine_connection }}{%- endcapture -%}
  {%- if machine_parms != "" -%}{%- capture machine_parms -%}{{ site.left_brace }}{{ machine_parms | prepend: "@" | replace: "@,","" }}{{ site.right_brace }}{%- endcapture -%}{%- endif -%}
  {%- if site.pcjs.uncompiled or site.pcjs.unbundled or machine.unbundled -%}
    {%- if site.pcjs.unbundled or machine.unbundled -%}
      {%- for script in machine_data.modules -%}
        {%- if script contains "/embed.js" -%}{% continue %}{%- endif -%}
        {%- capture page_script %}
    <script type="module" src="{{ site.baseurl }}/{{ script | replace: './','' }}"></script>
        {%- endcapture -%}
        {%- unless page_scripts contains page_script -%}
          {{ page_script }}
        {%- endunless -%}
        {%- capture page_scripts -%}{{ page_scripts }}{{ page_script }}{%- endcapture -%}
      {%- endfor -%}
    {%- else -%}
      {%- capture page_script %}
    <script src="{{ site.baseurl }}/machines/{{ machine_folder }}/releases/{{ machine_version }}/{{ machine_type }}-uncompiled.js"></script>
      {%- endcapture -%}
      {%- unless page_scripts contains page_script %}
        {{ page_script }}
      {%- endunless -%}
      {%- capture page_scripts -%}{{ page_scripts }}{{ page_script }}{%- endcapture -%}
    {%- endif -%}
  {%- else -%}
    {%- capture page_script -%}
    {%- if machine_data.script %}
    <script src="{{ site.baseurl }}{{ machine_data.script }}"></script>
    {%- else %}
    <script src="{{ site.baseurl }}/machines/{{ machine_folder }}/releases/{{ machine_version }}/{{ machine_type }}{%- if machine.uncompiled -%}-uncompiled{%- endif -%}.js"></script>
    {%- endif -%}
    {%- endcapture -%}
    {%- unless page_scripts contains page_script -%}
      {{ page_script }}
    {%- endunless -%}
    {%- capture page_scripts -%}{{ page_scripts }}{{ page_script }}{%- endcapture -%}
  {%- endif -%}
  {%- if machine_config contains ".xml" and machine_template == "" -%}
    {%- capture machine_template -%}{{ site.baseurl }}/machines/{{ machine_folder }}/xsl/components.xsl{%- endcapture -%}
  {%- endif -%}
  {%- if machine_factory contains "embed" %}
    {%- if site.pcjs.unbundled or machine.unbundled %}
    <script type="module">
      import { {{ machine_factory }}, globals } from "/machines/modules/v2/embed.js";
      {%- if site.pcjs.localdisks %}
      globals.window['LOCALDISKS'] = true;
      {%- endif %}
      {{ machine_factory }}('{{ machine_id }}','{{ machine_config }}','{{ machine_template }}','{{ machine_parms }}'{{ machine_class }});
    </script>
    {%- else %}
    <script>{% if site.pcjs.localdisks %}LOCALDISKS = true; {% endif %}{{ machine_factory }}('{{ machine_id }}','{{ machine_config }}','{{ machine_template }}','{{ machine_parms }}'{{ machine_class }});</script>
    {%- endif %}
  {%- elsif site.pcjs.unbundled or machine.unbundled %}
    <script type="module">
      {%- if site.pcjs.localdisks %}
      import { globals } from "/machines/modules/v2/defines.js";
      globals.window['LOCALDISKS'] = true;
      {%- endif %}
      PCjsMachine('{{ machine_id }}','{{ machine_config | strip_newlines }}','{{ machine_parms }}');
    </script>
  {%- elsif site.pcjs.uncompiled or machine.uncompiled %}
    <script>{% if site.pcjs.localdisks %}LOCALDISKS = true; {% endif %}PCjsMachine('{{ machine_id }}','{{ machine_config | strip_newlines }}','{{ machine_parms }}');</script>
  {%- else %}
    <script>{% if site.pcjs.localdisks %}LOCALDISKS = true; {% endif %}{{ machine_factory }}('{{ machine_id }}','{{ machine_config | strip_newlines }}','{{ machine_parms }}');</script>
  {%- endif %}
  {%- if machine.sticky == "top" %}
    <script src="{{ site.baseurl }}/machines/modules/v2/sticky.js"></script>
    <script>addStickyMachine('{{ machine_id }}');</script>
  {%- endif %}
{%- endfor %}